﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml.Linq;
using University.Workshop.Business;
using System.Text.RegularExpressions;
using University.Workshop.Model;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace University.Workshop.Presenters
{
    public class PresenterGifts
    {
        private IGifts Report;
        public PresenterGifts(IGifts report)
        {
            Report = report;  
        }


        public bool AddGift()
        {
            if (!((Regex.IsMatch(this.Report.NameGift, @"^[A-Za-z0-9 .#]*$")) && this.Report.NameGift.Length < 41 && this.Report.NameGift.Length > 0))
            {
                throw new BusinessException("Nombre del premio Invalido");
            }
            try
            {
                Convert.ToDouble(this.Report.PriceGift, System.Globalization.CultureInfo.CurrentCulture);
            }
            catch (Exception ex) {
                throw new BusinessException("Existe Un Error en el Formate de ingreso de datos Ej: xx,xx", ex);
            }
            GiftsControl.AddGift(this.Report.NameGift, Convert.ToDouble(this.Report.PriceGift, System.Globalization.CultureInfo.CurrentCulture).ToString(System.Globalization.CultureInfo.CurrentCulture));
            ChargeDataTable();
            return true;
        }
        public bool ChargeDataTable() {
            this.Report.TableGifts = GiftsControl.GetAllGifts(string.Empty);
            return true;
        }
        public static bool GenerateReport()
        {
            DataTable table= GiftsControl.GetAllGifts("todos");
            DataTable tableformat = FormatTableReport(table);
            table = tableformat;
            Document document = new Document();
            string nameFile="reporteCompraTodos.pdf";
            FileStream file = new FileStream(nameFile, FileMode.OpenOrCreate);
            try
            {
                PdfWriter.GetInstance(document, file);
                document.Open();
                Paragraph paragraphLogo = new Paragraph();
                Paragraph paragraphTitle = new Paragraph();
                Paragraph paragraphUser = new Paragraph();
                Paragraph paragraphSubtitle = new Paragraph();
                Chunk logo = new Chunk("Pag " + (document.PageNumber + 1) + "                                                                                          FARMACIA                                               Fecha: " + System.DateTime.Now.ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 9));
                Chunk title = new Chunk("Reporte de Premios A Clientes", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 20));
                Chunk subtitle = new Chunk("Todos", FontFactory.GetFont(FontFactory.HELVETICA, 16));
                Chunk userReport = new Chunk("Reporte generado por: " + UserLogOn.Instance.Name + " " + UserLogOn.Instance.Surname1 + " " + UserLogOn.Instance.Surname2, FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 8));
                paragraphLogo.Add(logo);
                paragraphTitle.Add(title);
                paragraphTitle.Alignment = Element.ALIGN_CENTER;
                paragraphSubtitle.Add(subtitle);
                paragraphSubtitle.Alignment = Element.ALIGN_CENTER;
                paragraphUser.Add(userReport);
                paragraphUser.Alignment = Element.ALIGN_LEFT;
                document.Add(paragraphLogo);
                document.Add(new Paragraph(" "));
                document.Add(paragraphTitle);
                document.Add(paragraphSubtitle);
                document.Add(paragraphUser);
                document.Add(new Paragraph(" "));
                document.Add(new Paragraph("---------------------------------------------------------------------------------------------------------------------------------"));
                PdfPTable tableformat1 = new PdfPTable(4);
                document.Add(new Paragraph(" "));
                Chunk h1 = new Chunk("Nombre", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                Chunk h2 = new Chunk("Precio", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                Chunk h3 = new Chunk("Fecha de Ingreso", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                Chunk h4 = new Chunk("Habilitado", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                tableformat1.AddCell(new Phrase(h1));
                tableformat1.AddCell(new Phrase(h2));
                tableformat1.AddCell(new Phrase(h3));
                tableformat1.AddCell(new Phrase(h4));
                for (int j = 0; j < tableformat.Rows.Count; j++)
                {
                    Chunk c1 = new Chunk(tableformat.Rows[j].ItemArray[0].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                    Chunk c2 = new Chunk(tableformat.Rows[j].ItemArray[1].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                    Chunk c3 = new Chunk(tableformat.Rows[j].ItemArray[2].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                    Chunk c4 = new Chunk(tableformat.Rows[j].ItemArray[3].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                    tableformat1.AddCell(new Phrase(c1));
                    tableformat1.AddCell(new Phrase(c2));
                    tableformat1.AddCell(new Phrase(c3));
                    tableformat1.AddCell(new Phrase(c4));
                }
                document.Add(tableformat1);
                document.Close();
                return true;
            }
            finally
            {
                if (file != null)
                    file.Dispose();
                if (document != null)
                    document.Dispose();
            }
           
        }

        private static DataTable FormatTableReport(DataTable table)
        {
            DataTable tableformat = new DataTable() { Locale = System.Globalization.CultureInfo.CurrentCulture };
            tableformat.Columns.Add("Nombre", typeof(string));
            tableformat.Columns.Add("Precio", typeof(string));
            tableformat.Columns.Add("Fecha de Ingreso", typeof(string));
            tableformat.Columns.Add("Habilitado", typeof(string));
            DataRow dr1 = tableformat.NewRow();

            foreach (DataRow dr in table.Rows)
            {
                dr1 = tableformat.NewRow();
                dr1["Nombre"] = dr["Nombre"];
                dr1["Precio"] = dr["Precio"];
                dr1["Fecha de Ingreso"] = dr["FechaDeIngreso"];
                if ((bool)dr["Habilitado"])
                {
                    dr1["Habilitado"] = "Si";
                }
                else
                {
                    dr1["Habilitado"] = "No";
                }
                tableformat.Rows.InsertAt(dr1,tableformat.Rows.Count);
            }
            table = tableformat;
            if (tableformat != null) {
                tableformat.Dispose();
            }
            return table;
        }

        public bool DeleteGift()
        {
            GiftsControl.DeleteGift(this.Report.NameGiftFromTable);
            ChargeDataTable();
            return true;   
        }
    }
}
